package com.qianyitian.hope2.analyzer.analyzer;

import com.qianyitian.hope2.analyzer.model.KLineInfo;
import com.qianyitian.hope2.analyzer.model.ResultInfo;
import com.qianyitian.hope2.analyzer.model.Stock;
import com.qianyitian.hope2.analyzer.util.AlgorithmUtil;
import com.qianyitian.hope2.analyzer.util.Utils;

import java.util.List;

//n天内有涨停，现价贴近5日线
public class SuddentIncreaseAnalyzer extends AbstractStockAnalyzer {

    // consider how many days before now.
    int daysToNow = 8;

    // increase range
    double increase = 9.9;

    public SuddentIncreaseAnalyzer() {
    }

    @Override
    public boolean analyze(ResultInfo resultInfo, Stock stock) {
        boolean ok = false;
        List<KLineInfo> infos = stock.getkLineInfos();
        if (infos.size() <= daysToNow + 1) {
            // 元数据天数要求大于考察天数
            return ok;
        }

        KLineInfo current = infos.get(infos.size() - 1);

        double ma5 = AlgorithmUtil.maN(infos, 5);

        if(current.getMacd()< infos.get(infos.size() - 2).getMacd()){
            return false;
        }

        if (current.getHigh() >= ma5 && current.getLow() <= ma5) {
            for (int i = infos.size() - daysToNow - 1; i < infos.size(); i++) {
                KLineInfo toCheck = infos.get(i);
                KLineInfo yesterday = infos.get(i - 1);
                double increaseReage = toCheck.getClose() / yesterday.getClose() - 1;

                boolean condition1 = increaseReage * 100 >= increase;
                if (condition1) {
                    String msg = format(stock, current, toCheck);
                    resultInfo.appendMessage(msg);
                    resultInfo.setValue(getTurnover(stock));
                    ok = true;
                    break;
                }
            }
        } else {
            return false;
        }
        return ok;
    }

    public String format(Stock stock, KLineInfo current, KLineInfo check) {
        StringBuilder sb = new StringBuilder();
        return (sb.toString());
    }

    @Override
    public String getDescription() {
        return daysToNow + "天之内有近似涨停，目前股价贴近5日线\n\r";
    }

    public int getDaysToNow() {
        return daysToNow;
    }

    public void setDaysToNow(int daysToNow) {
        this.daysToNow = daysToNow;
    }

    public int getWeight() {
        return 50;
    }

}
